package com.pubinfo.passbook.api.controller.contract;

import com.alibaba.excel.EasyExcel;
import com.pubinfo.passbook.api.eip.UserInfoService;
import com.pubinfo.passbook.common.mapper.TContractBaseMapper;
import com.pubinfo.passbook.common.model.dto.contract.ClaimedList;
import com.pubinfo.passbook.common.model.dto.contract.ContractClaerParam;
import com.pubinfo.passbook.common.model.dto.contract.ContractClearResultList;
import com.pubinfo.passbook.common.model.dto.contract.ContractTrialParam;
import com.pubinfo.passbook.common.model.dto.system.RolePermission;
import com.pubinfo.passbook.common.model.http.eip.UserInfo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.base.MyPage;
import com.pubinfo.passbook.common.service.contract.TContractBaseService;
import com.pubinfo.passbook.common.service.master.ITFileChangeUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;

/**
 * @Author litiewang
 * @Date 2022/6/27 10:13
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(tags = "QGPB-合同清洗报表")
@RequestMapping(value = "contract-trial")
public class ContractTrialReportController {
  @Resource
  private UserInfoService userInfoService;
  @Resource
  TContractBaseMapper baseMapper;
  @Resource
  private TContractBaseService contractBaseService;
  @Resource
  ITFileChangeUnitService itFileChangeUnitService;
  
  @PostMapping("ClaimedList")
  @ApiOperation(value = "已认领合同情况查询")
  public JsonResult<MyPage<ClaimedList>> ClaimedList(@RequestBody ContractTrialParam param) {
    UserInfo userInfo = userInfoService.getCurrentUserInfo();
    param.setRoleCode(userInfo.getRoleCode());
    param.setAcct(userInfoService.getCurrentAcct());
    param.setOrgId(userInfo.getOrgId());
    JsonResult<MyPage<ClaimedList>> success = JsonResult.success(contractBaseService.getContractClaimedList(param));
    return success;
  }
  
  @SneakyThrows
  @GetMapping("download")
  @ApiOperation(value = "下载已认领合同")
  public void download(HttpServletResponse response) {
    // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
    String fileName = URLEncoder.encode("已认领合同", "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
    ContractTrialParam param = new ContractTrialParam();
    UserInfo userInfo = userInfoService.getCurrentUserInfo();
    param.setRoleCode(userInfo.getRoleCode());
    param.setAcct(userInfoService.getCurrentAcct());
    param.setOrgId(userInfo.getOrgId());
    List<ClaimedList> baseList = contractBaseService.getContractDownloadExcal(param);
    List<String> includeColumnFiledNames = Arrays.asList(new String[]{"signDate", "endDate", "attachment"});
    EasyExcel.write(response.getOutputStream(), ClaimedList.class).excludeColumnFiledNames(includeColumnFiledNames).sheet("已认领合同").doWrite(baseList);
  }
  
  @PostMapping("ContractClearResult")
  @ApiOperation(value = "合同清洗的结果统计")
  public JsonResult<MyPage<ContractClearResultList>> ContractClearResult(@RequestBody ContractClaerParam param) {
    UserInfo userInfo = userInfoService.getCurrentUserInfo();
    param.setRoleCode(userInfo.getRoleCode());
    param.setAcct(userInfoService.getCurrentAcct());
    param.setOrgId(userInfo.getOrgId());
    JsonResult<MyPage<ContractClearResultList>> list = JsonResult.success(contractBaseService.selectContractClearResultList(param));
    return list;
  }
  
  @GetMapping("company-list")
  @ApiOperation(value = "合同中的公司列表")
  public JsonResult<List<String>> company(String keyword) {
    RolePermission param = new RolePermission();
    UserInfo userInfo = userInfoService.getCurrentUserInfo();
    param.setRoleCode(userInfo.getRoleCode());
    param.setAcct(userInfoService.getCurrentAcct());
    param.setOrgId(userInfo.getOrgId());
    List<String> companyList = baseMapper.listCompany(keyword, param);
    return JsonResult.success(companyList);
  }
  
  @SneakyThrows
  @PostMapping("downloadClearResult")
  @ApiOperation(value = "下载合同清洗的结果统计")
  public void downloadClearResult(HttpServletResponse response, @RequestBody ContractClaerParam param) {
    // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
    response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    response.setCharacterEncoding("utf-8");
    // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
    String fileName = URLEncoder.encode("合同清洗的结果统计", "UTF-8");
    response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
  
    List<ContractClearResultList> baseList = contractBaseService.getContractClearResulDownloadExcal(param);
  
    EasyExcel.write(response.getOutputStream(), ContractClearResultList.class).sheet("合同清洗的结果统计").doWrite(baseList);
  }

}
